package com.ly.security;

import com.ly.base.LoginUserUtil;
import com.ly.entity.User;
import com.ly.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationCredentialsNotFoundException;
import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.authentication.encoding.Md5PasswordEncoder;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;

/**
 * 登录
 * 自定义认证实现
 * @author ：LY
 * @date ：Created in 2020/11/8 20:01
 * @modified By：
 */
public class AuthProvider implements AuthenticationProvider {
    @Autowired
    private IUserService userService;

    private final Md5PasswordEncoder passwordEncoder = new Md5PasswordEncoder();
    @Override
    public Authentication authenticate(Authentication authentication) throws AuthenticationException {
        String userName = authentication.getName();

        String inputPassword = (String) authentication.getCredentials();
        User user;
        if(LoginUserUtil.checkTelephone(userName)){
            user = userService.findUserByTelephone(userName);
        }else{
             user =userService.findUserByName(userName);
        }

        if (user == null){
            throw new AuthenticationCredentialsNotFoundException("authError");
        }
        if (this.passwordEncoder.isPasswordValid(user.getPassword(),inputPassword,user.getId())){
            return new UsernamePasswordAuthenticationToken(user, null, user.getAuthorities());
        }
        throw  new BadCredentialsException("authError");
    }

    @Override
    public boolean supports(Class<?> aClass) {
        return true;
    }
}
